package com.wanxi.controller;

import com.wanxi.entity.FacilityClassifyEntity;
import com.wanxi.entity.HealthServiceStrenthEntity;
import com.wanxi.entity.NewsContentEntity;
import com.wanxi.entity.PageClassifyEntity;
import com.wanxi.service.FacilityClassifyService;
import com.wanxi.service.HealthServiceStrenthService;
import com.wanxi.service.NewsContentService;
import com.wanxi.service.PageClassifyService;
import com.wanxi.service.impl.FacilityClassifyServiceImpl;
import com.wanxi.service.impl.HealthServiceStrenthServiceImpl;
import com.wanxi.service.impl.NewsContentServiceImpl;
import com.wanxi.service.impl.PageClassifyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * home:首页
 * @Author : 林子淳
 * @create 2023/2/21 10:05
 */

@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1）处理乱码
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //2）得到前端数据
        //3）封装到model（实体类JavaBean）
        //4）调用逻辑层处理逻辑关系,将获得的数据通过JSON以对象的形式传到前端页面;
        //导航栏
        PageClassifyService pageClassifyService = new PageClassifyServiceImpl();
        //医疗设备
        FacilityClassifyService facilityClassifyService = new FacilityClassifyServiceImpl();
        //健康服务优势
        HealthServiceStrenthService healthServiceStrenthService = new HealthServiceStrenthServiceImpl();
        //新闻信息
        NewsContentService newsContentService = new NewsContentServiceImpl();

        //        pageList.get(1);
        //        String pageList = JSONObject.toJSONString(pageClassifyService.selectServic());
        //5）处理完后，得到结果
        //导航栏
        List<PageClassifyEntity> pageList = pageClassifyService.selectServic();
        req.setAttribute("pageList",pageList);
        //医疗设备
        List<FacilityClassifyEntity> facilityList = facilityClassifyService.selectServic();
        req.setAttribute("facilityList",facilityList);
        //健康服务优势
        List<HealthServiceStrenthEntity> healthList = healthServiceStrenthService.selectServic();
        req.setAttribute("healthList",healthList);
        //新闻信息
        List<NewsContentEntity> newsContentList = newsContentService.selectServic();
        req.setAttribute("newsContentList",newsContentList);
        //6）返回给前端：页面转发
        req.getRequestDispatcher("/jsp/home.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
